package edu.chinasoft.listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Application Lifecycle Listener implementation class OnLineForListener
 *
 */

public class OnLineForListener implements HttpSessionListener {

    /**
     * Default constructor. 
     */
    public OnLineForListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    //当触发创建session对象就执行
    public void sessionCreated(HttpSessionEvent arg0)  { 
         // TODO Auto-generated method stub
    	 //获取serveltcontext里数据
    	HttpSession session = arg0.getSession();
    	ServletContext servletContext = session.getServletContext();
    	Integer number = (Integer)servletContext.getAttribute("online");
    	
    	//数据+1,  重新保存到servletcontext对象里
    	if(number==null) {
    		servletContext.setAttribute("online", 1);
    	}else {
    		servletContext.setAttribute("online", number+1);
    	}
    	
    	
    }

	/**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    //当触发销毁session对象执行
    public void sessionDestroyed(HttpSessionEvent arg0)  { 
         // TODO Auto-generated method stub
   	 //获取serveltcontext里数据
   	HttpSession session = arg0.getSession();
   	ServletContext servletContext = session.getServletContext();
   	Integer number = (Integer)servletContext.getAttribute("online");
   	
   	//数据-1,  重新保存到servletcontext对象里

   		servletContext.setAttribute("online", number-1);

    	
    	
    	
    }
	
}
